package com.sanrenzu.everybodytv.ui.recommend_umeng;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.db.Collecion;
import com.umeng.socialize.UMAuthListener;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class RecommendLandActivity extends AppCompatActivity {
    @BindView(R.id.imageView_finish)
    ImageView imageViewFinish;
    @BindView(R.id.editText_userName)
    EditText editTextUserName;
    @BindView(R.id.editText_passWord)
    EditText editTextPassWord;
    @BindView(R.id.imageView_qq_lend)
    ImageView imageViewQqLend;
    @BindView(R.id.imageView_weibo_lend)
    ImageView imageViewWeiboLend;
    @BindView(R.id.imageView_weixin_lend)
    ImageView imageViewWeixinLend;
    private Unbinder bind;
    private UMShareAPI mShareAPI;
    private SharedPreferences.Editor editor = null;
    //声明常量
    private static final boolean LOGIN_SUCCESS = true;
    //声明bean类
    private Collecion loginbean;
    private static final String LOHGIN="AA";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_land);
        bind = ButterKnife.bind(this);
        mShareAPI = UMShareAPI.get(this);

        initOther();
    }

    private void initOther() {
        imageViewFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
        //
    }


    //QQ登陆
    public void clickQQ(View view){
        final boolean install = mShareAPI.isInstall(this, SHARE_MEDIA.QQ);
        if(install){
            //授权
            SHARE_MEDIA platform = SHARE_MEDIA.QQ;
            mShareAPI.doOauthVerify(this, SHARE_MEDIA.QQ, new UMAuthListener() {
                @Override
                public void onComplete(SHARE_MEDIA platform, int action, Map<String, String> data) {
                    Toast.makeText(getApplicationContext(), "登陆成功", Toast.LENGTH_SHORT).show();
                    //获取平台信息
                    mShareAPI.getPlatformInfo(RecommendLandActivity.this, SHARE_MEDIA.QQ, new UMAuthListener() {
                        @Override
                        public void onComplete(SHARE_MEDIA share_media, int i, final Map<String, String> map) {
                            //向SharePreference中传值
                            loginbean = new Collecion();
                            loginbean.setUrl(map.get("profile_image_url"));
                            loginbean.setName(map.get("screen_name"));
                            //初始化共享首选项
//                            getPreferences(MODE_PRIVATE);创建名字是类名的xml
                            SharedPreferences prefs=getSharedPreferences(LOHGIN,MODE_PRIVATE);
                            //初始化编辑器
                            editor = prefs.edit();
                            editor.putString("url",map.get("profile_image_url"));
                            editor.putString("name",map.get("screen_name"));
                            editor.commit();
                            finish();
//                            Collecion.BmobThirdUserAuth authInfo = new BmobUser.BmobThirdUserAuth(BmobUser.BmobThirdUserAuth.SNS_TYPE_QQ, data.get("access_token"), data.get("expires_in"), map.get("openid"));
//
//                            Collecion.loginWithAuthData(authInfo, new LogInListener<JSONObject>() {
//                                @Override
//                                public void done(JSONObject jsonObject, BmobException e) {
//                                    if(jsonObject!=null){
//                                        Collecion currentUser = BmobUser.getCurrentUser(Collecion.class);
//                                        currentUser.setUrl(map.get("profile_image_url"));
//                                        currentUser.update();
//                                        finish();
//                                    }
//                                }
//                            });
                        }

                        @Override
                        public void onError(SHARE_MEDIA share_media, int i, Throwable throwable) {
                            Toast.makeText(getApplicationContext(), "登陆错误", Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onCancel(SHARE_MEDIA share_media, int i) {
                            Toast.makeText(getApplicationContext(), "取消登陆", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @Override
                public void onError(SHARE_MEDIA platform, int action, Throwable t) {
                    Toast.makeText(getApplicationContext(), "登陆错误", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onCancel(SHARE_MEDIA platform, int action) {
                    Toast.makeText(getApplicationContext(), "取消登陆", Toast.LENGTH_SHORT).show();
                }
            });
        }
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }
}
